package com.zxl.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysOrderMapper;
import com.zxl.common.mapper.SysPayParametersMapper;
import com.zxl.common.model.WxV3payConfig;
import com.zxl.common.model.pojo.SysMember;
import com.zxl.common.model.pojo.SysOrder;
import com.zxl.common.model.pojo.SysPayParameters;
import com.zxl.common.service.SysOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zxw
 * @since 2023-03-30
 */
@Slf4j
@Service
public class SysOrderServiceImpl extends ServiceImpl<SysOrderMapper, SysOrder> implements SysOrderService {
    @Resource
    private SysPayParametersMapper sysPayParametersMapper;
    @Resource
    private WxV3payConfig wxV3payConfig;

    @Override
    public void countReceivedAmount(SysOrder sysOrder) {
        SysPayParameters sysPayParameters = sysPayParametersMapper.selectOne(new QueryWrapper<SysPayParameters>()
                .eq("mch_id", wxV3payConfig.getAppletMchid()));
        if (sysPayParameters == null){
            throw new BusinessException("手续费获取异常");
        }
        log.info("处理手续费");
        BigDecimal payPrice = sysOrder.getPayPrice();
        BigDecimal serviceChargePercent = sysPayParameters.getServiceCharge().divide(new BigDecimal("100")).setScale(10, RoundingMode.HALF_UP);
        /**  手续费  **/
        BigDecimal serviceCharge = payPrice.multiply(serviceChargePercent).setScale(2, BigDecimal.ROUND_HALF_UP);
        /**  实收金额  **/
        BigDecimal realPrice = payPrice.subtract(serviceCharge);
        sysOrder.setChargePrice(serviceCharge);
        sysOrder.setOfficialReceiptsPrice(realPrice);
    }
}
